home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / applic / ntp / acts.zoo / rdbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-14  |  4.0 KB  |  150 lines

  1. int rdbuf(ibuf,ie1,ie2,ie3,tmo)
  2. char ibuf[],ie1,ie2,ie3;
  3. int tmo;
  4. {
  5. /*
  6.         this subroutine reads characters from the serial line.  the
  7.     SUN version uses a loop on the system read call.  for the
  8.     IBMPC version if BIOS is defined, the read is done using
  9.     interrupt calls to the port defined by cmport.  if BIOS is not
  10.      defined, direct calls are made to the hardware using inport and
  11.     outport at the hardware address cmadr.  the characters are stored
  12.     in ibuf.  the operation continues until any one of the three line
  13.         terminators ie1, ie2 or ie3 is found.  the operation also ends
  14.         on the 280th character if no line terminator is found before that.
  15.         the input line is terminated by a zero byte; the last
  16.         character before the zero byte is the terminator.
  17.  
  18.         the parameter tmo is a time-out count.  if no character is
  19.         received before tmo goes to zero, the subroutine returns with
  20.         a count of zero (a time-out on a partial line will result in
  21.         the line being thrown away).
  22.  
  23.         the comparison against the terminating characters is made after
  24.         the 8th bit is cleared in the received character.  Thus if a
  25.         terminating character has its 8th bit on (i.e. is negative),
  26.         then it will never be matched and is effectively removed from
  27.         the comparison.
  28. */
  29. #include "nbstime.h"
  30. #include <stdio.h>
  31. #ifdef IBMPC
  32. #include <dos.h>
  33. #endif
  34. #ifdef SUN
  35. #include <sys/ioctl.h>
  36. #endif
  37. extern int debug;
  38. int j,tcount;
  39. #ifdef IBMPC
  40. unsigned char stat,mstat;
  41. int tlimit = -16000;
  42. #endif
  43. char cc;
  44. #ifdef IBMPC
  45. #ifdef BIOS
  46. extern int cmport;
  47. #endif
  48. #ifndef BIOS
  49. extern int cmadr;
  50. #endif
  51. #endif
  52. #ifdef SUN
  53. extern int cmport;
  54. long int k;
  55. int tlimit = -1000;
  56. #endif
  57.         tcount= tlimit;
  58.         j=0;
  59. #ifdef SUN
  60. /*
  61.     loop until character is ready or tcount goes to 0
  62. */
  63.     do
  64.     {
  65.        do
  66.        { 
  67.           ioctl(cmport,FIONREAD,&k);
  68.           tcount++;
  69.        } while( (tcount < 0 ) && (k < 1) ) ;
  70.        if(tcount == 0)
  71.           {
  72.           tcount=tlimit;
  73.           tmo++;
  74.           if(tmo == 0) return (0);
  75.           }
  76.        else
  77.           {
  78.           read(cmport,&cc,1);
  79.           ibuf[j]= cc = cc & 0x7f;
  80.           j++;
  81.           tcount=tlimit;
  82.           }
  83.     } while( (j < 280) && (cc != ie1) && (cc != ie2) && (cc != ie3) );
  84. #endif
  85. #ifdef IBMPC
  86.         do
  87.         {
  88.            do
  89.            {
  90. /*
  91.         loop until character is available or tcount goes to 0.
  92. */
  93.            #ifdef BIOS
  94.            _AH=3;
  95.            _DX=cmport;
  96.            geninterrupt(0x14);
  97.            stat=_AH;
  98.            mstat=_AL;
  99.        #endif
  100.        #ifndef BIOS
  101.            stat=inportb(cmadr+lsreg);     /* read line status*/
  102.            mstat=inportb(cmadr+msreg);    /* read modem status*/
  103.            #endif
  104.            tcount++;
  105.            }  while ( (tcount < 0) && ( (stat & 1) ==0) );
  106. /*
  107.         if we exited on a time out, increment time out counter, return
  108.         0 if number of time outs has expired, else go around again
  109. */
  110.         if(tcount == 0)
  111.            {
  112.            tmo++;
  113.            if(tmo == 0) return(0);
  114.            }
  115.         else
  116. /*
  117.         if tcount !=0 we exited on a char. available.  now read it,
  118.         turn off m. s. bit, store in array and advance pointer
  119. */
  120.            {
  121.            #ifdef BIOS
  122.            _AH=2;
  123.            _DX=cmport;
  124.            geninterrupt(0x14);
  125.            cc=_AL;
  126.        #endif
  127.        #ifndef BIOS
  128.            cc=inportb(cmadr);
  129.            #endif
  130.            ibuf[j]= cc= cc & '\x7f';    /* turn off ms bit */
  131.            j++;
  132. /*
  133.         if clear to send and carrier detect have been lost then
  134.         the fact that a character is ready to be read is an error
  135.         terminate line here. how can the hardware do this over
  136.         and over again ??
  137. */
  138.            if( (mstat && 0xb0) == 0)
  139.               {
  140.               ibuf[j]=0;
  141.               return(j);
  142.               }
  143.            }
  144.         tcount=tlimit;
  145.         } while ( (j < 280) && (cc != ie1) && (cc != ie2) && (cc != ie3));
  146. #endif
  147.         ibuf[j]=0;
  148.         return(j);
  149. }
  150.